#include"main_server.h"

// 针对API发送的获取host消息所做的响应函数
void get_host_cb(const char *data, uint32_t len, int msgid, net_connection* net_conn, void *args){
    printf("get_host_cb recv from api message\n");
    route_lb * route_lb_p =  (route_lb *)args;
    
    lars::GetHostRequest request;
    request.ParseFromArray(data, len);

    // 取出API发送的消息
    int modid = request.modid();
    int cmdid = request.cmdid();

    
    // 组装回复API的消息
    lars::GetHostResponse response;
    response.set_seq(request.seq());
    response.set_modid(modid);
    response.set_cmdid(cmdid);

    response.set_retcode(lars::RET_SUCC);

    // 从server端获取modid和cmdid对应的host消息
    route_lb_p->get_host(modid, cmdid, response);

    // 将消息转换成字符串
    std::string responseString;
    response.SerializeToString(&responseString);

    // 尝试将消息请求发给API客户端
    net_conn->send_message(responseString.c_str(), responseString.size(), lars::ID_GetHostResponse);


}

// 针对API发过来的host主机调用情况进行上报的回调函数
void report_cb(const char *data, uint32_t len, int msgid, net_connection* net_conn, void *args){
    printf("report_cb recv from api message\n");
    route_lb * route_lb_p =  (route_lb *)args;
    
    lars::ReportRequest request;
    request.ParseFromArray(data, len);

    // 上报host的调用结果
    route_lb_p->report_host(request);



}

// 针对API发过来的获取全部host的信息的回调函数
void get_route_cb(const char *data, uint32_t len, int msgid, net_connection* net_conn, void *args){
    printf("get_route recv from api message\n");
    route_lb * route_lb_p =  (route_lb *)args;
    
    lars::GetRouteRequest request;
    request.ParseFromArray(data, len);

    // 取出API发送的消息
    int modid = request.modid();
    int cmdid = request.cmdid();

    
    // 组装回复API的消息
    lars::GetRouteResponse response;
    response.set_modid(modid);
    response.set_cmdid(cmdid);


    // 从server端获取modid和cmdid对应的host消息
    route_lb_p->get_route(modid, cmdid, response);

    // 将消息转换成字符串
    std::string responseString;
    response.SerializeToString(&responseString);

    // 尝试将消息请求发给API客户端
    net_conn->send_message(responseString.c_str(), responseString.size(), lars::ID_API_GetRouteResponse);
}

// 一个UDP线程的回调函数
void *agent_server_main(void* args){
    // 如果port修改，则前端也要修改
    static int  index = 0;
    int port = 8888 + index;
    index++;
    
    event_loop loop;

    udp_server server(&loop, config.udp_server_ip.c_str(), port);

    // 给UDPserver注册一些路由回调
    // 8888->r_lb[0], 8889->r_lb[1], 8888->r_lb[2], 
    // 给udp server注册一个读API发过来的需要调用host的请求
    server.add_msg_router(lars::ID_GetHostRequest, get_host_cb, r_lb[port-8888]);
    
    // 给udp server注册一个读API发过来的host调用结果的回调函数
    server.add_msg_router(lars::ID_ReportRequest, report_cb, r_lb[port-8888]);

    // 给udp server注册一个读API发过来请求当前mod下的所有host信息的回调函数
    server.add_msg_router(lars::ID_API_GetRouteRequest, get_route_cb, r_lb[port-8888]);

    loop.event_process();

    return NULL;
}


// 启动多个UDP服务线程
void start_udp_server(void* args){
    
    for(int i=0; i<config.udp_server_cnt; i++){
        pthread_t tid;
        
        // 创建线程
        int ret = pthread_create(&tid, NULL, agent_server_main, NULL);
        if(ret == -1){
            perror("pthread_create error\n");
            exit(1);
        }

        // 设置线程分离
        pthread_detach(tid);

    }
}